package lp.polimi.treefinder;

import java.util.Locale;

import lp.polimi.treefinder.utils.UIUtils;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

/** 
 * The fragment calls the Wikipedia mobile API to retrieve additional data related to the tagged species.
 * The embedded webview displays the result.
 */
public class WikipediaFragment extends Fragment {

	private WebView wv;
	
	public WikipediaFragment() {
		// Required empty public constructor
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_wikipedia, container, false);
	}

	
	@Override
	public void onActivityCreated(final Bundle savedInstanceState) {
		
		String leafName = getActivity().getIntent().getExtras().getString(XMLHandler.KEY_NAME);
		// The language is dependent on the current locale (English only, for now).
		final String url = "http://"+Locale.getDefault().getLanguage()+".m.wikipedia.org/wiki/"+leafName.substring(0, leafName.indexOf(" "));
		
		// Switch to the web browser, loading the same URL.
		Button showURLButton = (Button) getActivity().findViewById(R.id.showURL_btn);
		showURLButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setData(Uri.parse(url));
				startActivity(i);
			}
		});
		
		
		super.onActivityCreated(savedInstanceState);
		wv = (WebView) getActivity().findViewById(R.id.webview);
		wv.setWebViewClient(new WebViewClient() {
			ProgressDialog pd = null;
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				return true;
			}
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				UIUtils.lockScreenRotation(getActivity());
				
				if (savedInstanceState == null)
					pd = ProgressDialog.show(getActivity(), getResources().getText(R.string.loading_page),
							getResources().getText(R.string.loading_page_message), true, true, new DialogInterface.OnCancelListener() {
								
								@Override
								public void onCancel(DialogInterface dialog) {
									wv.stopLoading();
									
								}
							});
			}
			@Override
			public void onPageFinished(WebView view, String url) {

				if (savedInstanceState == null)
					if(pd.isShowing()&&pd!=null) {
						pd.dismiss();
					}
				UIUtils.unlockScreenRotation(getActivity());
			}
			@Override
			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
				if(pd!=null && pd.isShowing()) {
					pd.dismiss();
				}
				UIUtils.unlockScreenRotation(getActivity());
			}
		});

		if (savedInstanceState != null) {
			wv.restoreState(savedInstanceState);
			UIUtils.unlockScreenRotation(getActivity());
		} else {
			
			wv.loadUrl(url);
			
		}
	}


	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (wv!=null)
			wv.saveState(outState);
	}


	
	
}
